package cn.com.surker.pages;

import cn.com.surker.common.AbstractController;
import cn.com.surker.common.Constant;
import cn.com.surker.enums.oa.CostTypeEnum;
import cn.com.surker.enums.oa.*;
import cn.com.surker.system.entity.SysOrigin;
import cn.com.surker.system.entity.SysUser;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.IErpOriginService;
import cn.com.surker.system.service.MenuInfoService;
import cn.com.surker.system.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lucky
 * @create 2021年03月19 9:40
 * @desc
 */

@Slf4j
@Controller
@RequestMapping("/cloud")
public class CloudIndexController extends AbstractController {

    /**菜单业务逻辑类**/
    @Reference
    private MenuInfoService menuInfoService;
    @Resource
	private IErpOriginService oaOriginService;
    @Resource
    private UserService userService;

	/*@Autowired
	private WhiteUserService whiteUserService;*/


    /**
     * 待办流程
     * @return
     */
    @RequestMapping("/todo-process")
    public String todoProcess(){

        return "cloud/process/todo-process";
    }

    /**
     * 经办流程
     * @return
     */
    @RequestMapping("/handle-process")
    public ModelAndView handleProcess(){
        ModelAndView modelAndView = new ModelAndView();

        List<Map<String, Object>> listMap = new ArrayList<>();
        for (int i = 0; i < ProcessStatusEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", ProcessStatusEnum.values()[i].getCode());
            map.put("name", ProcessStatusEnum.values()[i].getDescription());
            listMap.add(map);
        }
        List<Map<String, Object>> processListMap = new ArrayList<>();
        for (int i = 0; i < ProcessBizEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", ProcessBizEnum.values()[i].getName());
            map.put("type", ProcessBizEnum.values()[i].getDescription());
            processListMap.add(map);
        }
        Map<String, String> processMap = Constant.processMap;
        modelAndView.addObject("processMap", processMap);
        modelAndView.addObject("statusList", listMap);
        modelAndView.addObject("processList", processListMap);
        modelAndView.setViewName("cloud/process/handle-process");
        return modelAndView;
    }



    /**
     * 抄送流程
     * @return
     */
    @RequestMapping("/copy-process")
    public ModelAndView copyProcess(){
        ModelAndView modelAndView = new ModelAndView();

        List<Map<String, Object>> listMap = new ArrayList<>();
        for (int i = 0; i < ProcessStatusEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", ProcessStatusEnum.values()[i].getCode());
            map.put("name", ProcessStatusEnum.values()[i].getDescription());
            listMap.add(map);
        }
        List<Map<String, Object>> processListMap = new ArrayList<>();
        for (int i = 0; i < ProcessBizEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", ProcessBizEnum.values()[i].getName());
            map.put("type", ProcessBizEnum.values()[i].getDescription());
            processListMap.add(map);
        }
        Map<String, String> processMap = Constant.processMap;
        modelAndView.addObject("processMap", processMap);
        modelAndView.addObject("statusList", listMap);
        modelAndView.addObject("processList", processListMap);
        modelAndView.setViewName("cloud/process/copy-process");
        return modelAndView;
    }

    /**
     * 生产运输付款
     * @return
     */
    @RequestMapping("/production-process")
    public String productionProcess(){

        return "cloud/production/production-process";
    }

    /**
     * 印章流程
     * @return
     */
    @RequestMapping("/seal-process")
    public String sealProcess(){

        return "cloud/seal/seal-process";
    }

    /**
     * 请假流程
     * @return
     */
    @RequestMapping("/leave-process")
    public String leaveProcess(){

        return "cloud/leave/leave-process";
    }

    /**
     * 申购流程
     * @return
     */
    @RequestMapping("/buy-process")
    public String buyProcess(){

        return "cloud/buy/buy-process";
    }

    /**
     * 补卡流程
     * @return
     */
    @RequestMapping("/supplement-process")
    public String supplementProcess(){

        return "cloud/supplement/supplement-process";
    }

    /**
     * 费用借支
     * @return
     */
    @RequestMapping("/borrow-process")
    public ModelAndView borrowProcess(){
    	ModelAndView modelAndView = new ModelAndView("cloud/borrow/borrow-process");
		List<Map<String, Object>> costTypeList = new ArrayList<>();
		for (int i = 0; i < CostTypeEnum.values().length; i++) {
			Map<String, Object> map = new HashMap<>();
			map.put("value", CostTypeEnum.values()[i].getCode());
			map.put("label", CostTypeEnum.values()[i].getDescription());
			costTypeList.add(map);
		}
		modelAndView.addObject("costTypeList", costTypeList);
        return modelAndView;
    }

    /**
     * 材料付款
     * @return
     */
    @RequestMapping("/material-process")
    public String materialProcess(){

        return "cloud/material/material-process";
    }

    /**
     * 费用报销
     * @return
     */
    @RequestMapping("/expenses-process")
    public ModelAndView expensesProcess(){
    	ModelAndView mv = new ModelAndView("cloud/expenses/expenses-process");
		List<Map<String, Object>> costTypeList = new ArrayList<>();
		for (int i = 0; i < CostTypeEnum.values().length; i++) {
			Map<String, Object> map = new HashMap<>();
			map.put("value", CostTypeEnum.values()[i].getCode());
			map.put("label", CostTypeEnum.values()[i].getDescription());
			costTypeList.add(map);
		}
		mv.addObject("costTypeList", costTypeList);
        return mv;
    }

    /**
     * 发票开票报销
     * @return
     */
    @RequestMapping("/invoice-process")
    public ModelAndView invoiceProcess(){
        ModelAndView mv = new ModelAndView("cloud/invoice/invoice-process");
        return mv;
    }

	/**
	 * 抵房流程
	 * @return
	 */
	@RequestMapping("/mortgage-process")
	public ModelAndView mortgageProcess(){
		ModelAndView mv = new ModelAndView("cloud/mortgage/mortgage-process");
		return mv;
	}


    /**
     * 登录页面
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView login() {
        CacheUser user = getUser();
        if (user == null) {
            return new ModelAndView("cloud/login");
        } else {
            ModelAndView view = new ModelAndView("cloud/index");
            view.addObject("url", user.getAvatarUrl());
            view.addObject("name", user.getName());
            view.addObject("phone", user.getPhone());
            view.addObject("position", user.getPosition());
            return view;
        }
    }

    /**
     * 旭科手机端首页
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(String url) {
        CacheUser user = getUser();
        if (user == null) {
            return new ModelAndView("cloud/login");
        } else {

            ModelAndView view = new ModelAndView();
            view.addObject("url", user.getAvatarUrl());
            view.addObject("name", user.getName());
            view.addObject("phone", user.getPhone());
            view.addObject("position", user.getPosition());
            if (user.getName().equals("曹友强") || user.getName().equals("袁其斌") || user.getName().equals("易洪英")
                    || user.getName().equals("系统管理员") || user.getName().equals("管理员")) {

                view.setViewName("cloud/copy/index");
                return view;
            }
            if (user.getRoleNameList().contains("系统管理员") || user.getRoleNameList().contains("软件工程师")
                    || user.getRoleNameList().contains("网络工程师") || user.getRoleNameList().contains("人力资源总监")
                    || user.getRoleNameList().contains("人力资源副总监") || user.getRoleNameList().contains("人力资源经理")) {
                view.addObject("isAdmin", 1);
            } else {
                view.addObject("isAdmin", 0);
            }
            view.setViewName("cloud/index");

            return view;
        }
    }

	/**
	 * 预览pdf文件页面
	 * @param fid
	 * @return
	 */
	@RequestMapping("/preview/pdf")
    public ModelAndView previewPdf(Long fid){
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("fid",fid);
    	mv.setViewName("cloud/file/preview-pdf");
    	return mv;
	}

	@RequestMapping("/camera/test")
	public String camera(){
		return "cloud/camera/test";
	}

    /**
     * 车辆信息登记
     * @return
     */
    @RequestMapping("/car-info")
    public ModelAndView carInfo(){
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
        if (user == null || user.getStatus() == 1) {
            modelAndView.setViewName("error/403");
            return modelAndView;
        }

        Boolean verify = isVerify("/cloud/car-info");
        if (verify) {
            modelAndView.addObject("isVerify", 1);
        } else {
            modelAndView.addObject("isVerify", 0);
        }

        List<SysOrigin> origins = oaOriginService.relationUserOrgList(user.getId());
        List<Map<String, Object>> typeList = new ArrayList<>();
        List<Map<String, Object>> qualityList = new ArrayList<>();
        for (int i = 0; i < CarTypeEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", CarTypeEnum.values()[i].getCode());
            map.put("name", CarTypeEnum.values()[i].getDescription());
            typeList.add(map);
        }
        for (int i = 0; i < CarQualityEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", CarQualityEnum.values()[i].getCode());
            map.put("name", CarQualityEnum.values()[i].getDescription());
            qualityList.add(map);
        }
        if (origins != null && origins.size() > 0) {
            SysOrigin origin = origins.get(0);
            if (origin.getChildren() != null && origin.getChildren().size() > 0) {
                modelAndView.addObject("orgList", origin.getChildren());
            }
        }
        modelAndView.addObject("typeList", typeList);
        modelAndView.addObject("qualityList", qualityList);
        modelAndView.setViewName("cloud/hr/car-info");
        return modelAndView;
    }

}
